<template>
  <div>
    <div>data:{{ data }}</div>
    <div><div id="container" style="height: 100%; width: 100%"></div></div>
  </div>
</template>

<script setup lang="ts">
import G6 from "@antv/g6";

import { onMounted, ref } from "vue";

const ICON_MAP = {
  a: "https://gw.alipayobjects.com/mdn/rms_8fd2eb/afts/img/A*0HC-SawWYUoAAAAAAAAAAABkARQnAQ",
  b: "https://gw.alipayobjects.com/mdn/rms_8fd2eb/afts/img/A*sxK0RJ1UhNkAAAAAAAAAAABkARQnAQ",
};

G6.registerNode(
  "card-node",
  {
    drawShape: function drawShape(cfg, group) {
      const r = 2;
      const shape = group.addShape("rect", {
        attrs: {
          x: 0,
          y: 0,
          width: 200,
          height: 60,
          stroke: cfg.color,
          radius: r,
        },
        // must be assigned in G6 3.3 and later versions. it can be any string you want, but should be unique in a custom item type
        name: "main-box",
        draggable: true,
      });

      group.addShape("rect", {
        attrs: {
          x: 0,
          y: 0,
          width: 200,
          height: 20,
          fill: cfg.color,
          radius: [r, r, 0, 0],
        },
        // must be assigned in G6 3.3 and later versions. it can be any string you want, but should be unique in a custom item type
        name: "title-box",
        draggable: true,
      });

      // left icon
      group.addShape("image", {
        attrs: {
          x: 4,
          y: 2,
          height: 16,
          width: 16,
          cursor: "pointer",
          img: ICON_MAP[cfg.nodeType || "app"],
        },
        // must be assigned in G6 3.3 and later versions. it can be any string you want, but should be unique in a custom item type
        name: "node-icon",
      });

      // title text
      group.addShape("text", {
        attrs: {
          textBaseline: "top",
          y: 4,
          x: 24,
          lineHeight: 20,
          text: cfg.title,
          fill: "#fff",
        },
        // must be assigned in G6 3.3 and later versions. it can be any string you want, but should be unique in a custom item type
        name: "title",
      });

      group.addShape("marker", {
        attrs: {
          x: 184,
          y: 40,
          r: 6,
          cursor: "pointer",
          symbol: cfg.collapse ? G6.Marker.expand : G6.Marker.collapse,
          stroke: "#666",
          lineWidth: 1,
        },
        // must be assigned in G6 3.3 and later versions. it can be any string you want, but should be unique in a custom item type
        name: "collapse-icon",
      });

      group.addShape("text", {
        attrs: {
          textBaseline: "top",
          y: 35,
          x: 24,
          lineHeight: 20,
          text: cfg.des,
          fill: "rgba(0,0,0, 0.4)",
        },
        // must be assigned in G6 3.3 and later versions. it can be any string you want, but should be unique in a custom item type
        name: `index-title-${1}`,
      });
      return shape;
    },
  },
  "single-node"
);

const testNode = {
  title: "发起人",
  nodeType: "a",
  id: "node6",
  color: "#576a95",
  des: "北京翊博生物科技有限公司",
  collapse: true,

  x: 100,
  y: 500,
};

const data = ref({
  // 点集
  nodes: [
    {
      title: "发起人",
      nodeType: "a",
      id: "node1",
      color: "#576a95",
      des: "北京翊博生物科技有限公司",
      collapse: true,
      x: 100,
      y: 100,
    },
    {
      title: "抄送人",
      color: "#3296fa",
      nodeType: "a",
      id: "node2",
      des: "北京翊博生物科技有限公司",
      collapse: true,
      x: 100,
      y: 200,
    },
    {
      title: "审批人",
      color: "#ff943e",
      nodeType: "a",
      id: "node3",
      des: "北京翊博生物科技有限公司",
      collapse: true,
      x: 100,
      y: 300,
    },
    {
      title: "",
      color: "#ccc",
      nodeType: "a",
      id: "node4",
      des: "结束任务",
      collapse: true,
      x: 100,
      y: 400,
    },
  ],
  // 边集
  edges: [
    {
      // label: 'test',
      source: "node1", // String，必须，起始点 id
      target: "node2", // String，必须，目标点 id
    },
    {
      source: "node2", // String，必须，起始点 id
      target: "node3", // String，必须，目标点 id
    },
    {
      source: "node3", // String，必须，起始点 id
      target: "node4", // String，必须，目标点 id
    },
  ],
});

onMounted(() => {
  // console.log(G6.Global.version)
  const container: any = document.getElementById("container");
  const width = container.scrollWidth;
  const height = container.scrollHeight || 500;
  const graph = new G6.Graph({
    container: "container",
    width,
    height,
    // type: 'dagre',
    // rankdir: 'TB', //  说明：布局的方向。T：top（上）；B：bottom（下）；L：left（左）；R：right（右）。
    // align: 'DR', // 节点对齐方式。U：upper（上）；D：down（下）；L：left（左）；R：right（右）
    // 节点不同状态下的样式集合

    modes: {
      default: ["zoom-canvas", "drag-node"],
      edit: ["click-select"],
    },
    defaultNode: {
      type: "card-node",
    },
    defaultEdge: {
      type: "polyline",
      style: {
        radius: 20,
        offset: 40,
        endArrow: true,
        lineWidth: 1,
        stroke: "#C2C8D5",
      },
    },
    // layout: {
    //   type: 'dagre',
    //   controlPoints: false, // 可选
    //   align: undefined, // 可选
    //   nodesep: 100, // 可选
    //   ranksep: 10 // 可选
    // }
    // fitView: true, // 设置是否将图适配到画布中；
    // fitViewPadding: [20, 40, 50, 20] // 画布上四周的留白宽度。
  });

  graph.data(data.value); // 读取 Step 2 中的数据源到图上
  graph.render(); // 渲染图

  // 监听鼠标点击节点
  graph.on("node:click", (e) => {
    // 先将所有当前有 click 状态的节点的 click 状态置为 false
    // const clickNodes = graph.findAllByState('node', 'click')
    // clickNodes.forEach((cn) => {
    //   graph.setItemState(cn, 'click', false)
    // })
    // const nodeItem = e.item
    // // 设置目标节点的 click 状态 为 true
    // graph.setItemState(nodeItem, 'click', true)

    console.log("eeee", e);
    graph.addItem("node", testNode);
  });
});
</script>
<!-- 
<style scoped lang="less">
@boxHeight: 80px; // 盒子高
@boxWidth: 220px; // 盒子宽
@titleHeight: 25px; // 标题高度
@lineColor: #ccc; // 连线颜色
@top-line-height: 30px; // 顶部线高
@bottom-line-height: 90px; // 底部线高

.item {
  width: @boxWidth;
  height: @boxHeight;
  border-radius: 5px;
  border: 1px solid @lineColor;
  box-shadow: 0px 3px 5px @lineColor;
  margin-bottom: @bottom-line-height;
  margin-left: 60px;
  margin-right: 60px;
  position: relative;
  margin-top: 20px;
  &.end-card-wrap {
    margin-top: @bottom-line-height - 10px;
  }
  .flat-line {
    top: -30px;
  }

  .title {
    font-size: 12px;
    height: @titleHeight;
    line-height: 25px;
    background-color: #576a95;
    border-radius: 5px 5px 0 0;
    color: #fff;
    padding-left: 10px;
  }
  .des {
    height: @boxHeight - @titleHeight;
    background-color: white;
    padding: 10px;
    border-radius: 0 0 5px 5px;
  }

  .top-line {
    height: @top-line-height;
    width: 1px;
    background-color: red;
    position: absolute;
    left: 50%;
    top: -@top-line-height;
    z-index: 0;
    position: absolute;
  }

  .bottom-line {
    height: @bottom-line-height;
    width: 1px;
    background-color: @lineColor;
    position: absolute;
    left: 50%;
    z-index: 10;

    &.end-card {
      top: -@bottom-line-height;
    }
    .triangle {
      width: 0;
      height: 0;
      border-left: 5px solid transparent;
      border-right: 5px solid transparent;
      border-top: 5px solid @lineColor;
      position: absolute;
      bottom: 0;
      left: -4px;
    }
    .add-btn {
      width: 80px;
      height: 30px;
      line-height: 30px;
      background-color: white;
      border-radius: 20px;
      box-shadow: 0px 3px 5px @lineColor;
      text-align: center;
      color: #277ace;
      font-size: 12px;
      left: -40px;
      position: absolute;
      bottom: -5px;
      z-index: 10;
      cursor: pointer;
    }
    .add-btn-icon {
      @size: 26px;
      width: @size;
      height: @size;
      line-height: @size - 4;
      border-radius: 50%;
      background-color: #3296fa;
      color: white;
      text-align: center;
      font-size: @size;
      padding: 0;
      position: absolute;
      left: -13px;
      top: 20px;
      cursor: pointer;
    }
  }
}
.wrap-parent,
.wrap-children {
  display: flex;
  justify-content: center;
  // 水平线
  .flat-line {
    height: 1px;
    // width: 110px + 55px;
    width: 100%;
    background-color: @lineColor;
    position: absolute;
    // z-index: 10;

    top: -10px;
    &.right {
      left: 50%;
    }
    &.left {
      right: 50%;
    }
    &.middle {
      left: -65px;
    }
  }
}
.wrap-parent {
  position: relative;
  .next-line {
    height: 1px;
    width: 110px + 55px;
    background-color: @lineColor;
    position: absolute;
    // z-index: 10;
    right: 50%;
    bottom: 0;
    .stand-line {
      position: absolute;
      height: 20px;
      width: 1px;
      background-color: @lineColor;
    }
    &.next-line-2-right {
      left: 50%;
      .stand-line {
        right: 0;
      }
    }
  }
}

.flat-line-bottom {
  width: 110px + 55px;

  background-color: @lineColor;
  height: 1px;
  position: absolute;
  top: -@bottom-line-height;
  &.left {
    left: 50%;
  }
  &.right {
    right: 50%;
  }
}

.layout-start {
  display: flex;
  justify-content: center;
}
.layout-route {
  display: flex;
  justify-content: space-between;
}
</style> -->
